//
//  CustomAlertView.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 6/26/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "CustomAlertView.h"
#import "DelFoodTableViewCell.h"
#import "ZooFoodBean.h"

@implementation CustomAlertView
@synthesize lbNumberOfFood = _lbNumberOfFood;
@synthesize sum,arFoodChecked,arFoodRemoveCheck;

-(id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        _mainView = [[UIView alloc]init];
//        [_mainView setBackgroundColor:[UIColor greenColor]];
//
//        UIButton *test = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//        test.frame = CGRectMake(10, 10, 200, 100);
//        [test addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
//        
//        [test setTitle:@"Test" forState:UIControlStateNormal];
//        [_mainView addSubview:test];
//        [self addSubview:_mainView];
        arFoodRemoveCheck = [[NSMutableArray alloc]init];
        sum = 0;
    }
    return self;
}

- (void)clicked {
    sum ++;
    NSLog(@"test clicked %d",sum);
    
}

- (void)layoutSubviews {
    
    for (UIView *subview in self.subviews){ //Fast Enumeration
                                            //NSLog(@"subview class :%@",[subview class]); //Get Class Description of Each Subview
                                            //NSLog(@"subview.tag %i",subview.tag); //Get Button Tags

		if ([subview isMemberOfClass:[UIImageView class]]) { //Find UIImageView Containing Blue Background
			subview.hidden = YES; //Hide UIImageView Containing Blue Background
                                  // [subview removeFromSuperview]; Also Works
		}
        
		if ([subview isMemberOfClass:[UILabel class]]) { //Point to UILabels To Change Text
			UILabel *label = (UILabel*)subview;	//Cast From UIView to UILabel
//			label.textColor = [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0f];
			label.shadowColor = [UIColor clearColor];
			label.shadowOffset = CGSizeMake(0, 0);
            label.textColor = [UIColor greenColor];
            label.font = [UIFont systemFontOfSize:18];
		}
        
        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            button.titleLabel.font = [UIFont systemFontOfSize:15];
            [button setBackgroundImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND] forState:UIControlStateNormal];
            button.tintColor = [UIColor blackColor];
            [button.titleLabel setTextColor:[UIColor blackColor]];
            
        }
	}
    [self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND]]];
}

- (void)drawRect:(CGRect)rect 
{
    _mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 284, 300)];
    [_mainView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND]]];
    
    _lbStringFood = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 180, 25)];
    [_lbStringFood setText:@"SO MON DA CHON"];
    [_lbStringFood setBackgroundColor:[UIColor clearColor]];
    
    _lbNumberOfFood = [[UILabel alloc]initWithFrame:CGRectMake(220, 10, 50, 25)];
    [_lbNumberOfFood setText:[NSString stringWithFormat:@"%d",[arFoodChecked count]]];
    [_lbNumberOfFood setTextColor:[UIColor redColor]];
    [_lbNumberOfFood setBackgroundColor:[UIColor clearColor]];
    
    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, 45, self.bounds.size.width, 1)];
    [line setImage:[UIImage imageNamed:IMG_DELFOOD_LINE]];
    
    _vDelFood = [[UITableView alloc]initWithFrame:CGRectMake(0, 46, self.bounds.size.width, 260)];
    [_vDelFood setBackgroundColor:[UIColor clearColor]];
    _vDelFood.delegate = self;
    _vDelFood.dataSource = self;
    
    [_mainView addSubview:line];
    [_mainView addSubview:_lbStringFood];
    [_mainView addSubview:_lbNumberOfFood];
    [_mainView addSubview:_vDelFood];
    [self addSubview:_mainView];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier0 = @"cellIdentifier0";
    DelFoodTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier0];    
    
    if (cell == nil) {
        cell = [[DelFoodTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier0];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    ZooFoodBean *foodBean = [[ZooFoodBean alloc]init];
    foodBean = [arFoodChecked objectAtIndex:indexPath.row];
    
    cell.lbFoodName.text = foodBean.foodName;
    cell.btnCheckbox.tag = indexPath.row;
    [cell.btnCheckbox addTarget:self action:@selector(btnCheckboxClicked:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [arFoodChecked count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

- (void)btnCheckboxClicked:(id)sender {
    DelFoodTableViewCell *cell = (DelFoodTableViewCell *)[sender superview];
    NSLog(@"%d",cell.btnCheckbox.tag);
    NSNumber *found = [[NSNumber alloc]init];

    for (NSNumber *i in arFoodRemoveCheck) {
        if ([i isEqualToNumber:[NSNumber numberWithInteger:cell.btnCheckbox.tag]]) {
            found = [NSNumber numberWithUnsignedInteger:[arFoodRemoveCheck indexOfObject:i]];
        }
    }
    
    if (found) {
        [arFoodRemoveCheck removeObjectAtIndex:[found unsignedIntegerValue]];
        [cell.vImgCheck setImage:nil];
    } else {
        [arFoodRemoveCheck addObject:[NSNumber numberWithInteger:cell.btnCheckbox.tag]];                        
        [cell.vImgCheck setImage:[UIImage imageNamed:IMG_DELFOOD_CHECKBOX_CHECKED]];
    }
    
    for (NSNumber *i in arFoodRemoveCheck) {
        NSLog(@"%d",[i integerValue]);
    }

}

@end

